// pages/confirmOrder/comfireOrder.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 缓存收货地址信息
    // var address = wx.getStorageSync("address");
    // console.log(address)
    // if (address) {
    //   this.setData({
    //     address: address
    //   })
    // }
    var url = app.globalData.ydnBase + '/index.php/Weshop/SApi/index/m/orderIndex/key/2sdfdd332342341dddff46e189670/wid/39';
    var that = this;
    wx.request({
      url: url,
      data: {
        sign: app.globalData.sign,
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      success: function (res) {
        that.processList(res)
      },
      fail: function (res) {
        console.log(res)
      }
    })



  },
  addAddress: function () {
    var that = this;
    wx.showModal({
      title: '温馨提示',
      content: '是否允许小程序获取您的收货地址,以便您能顺利收到商品',
      success: function (res) {
        if (res.confirm) {
          wx.chooseAddress({
            success: function (res) {
              that.setData({
                address: res
              })
              var url = app.globalData.ydnBase + '/index.php/Weshop/SApi/index/key/2sdfdd332342341dddff46e189670/m/saveAddress/wid/39/sign/' + app.globalData.sign;
              wx.request({
                url: url,
                data: {
                  userName: res.userName,
                  provinceName: res.provinceName,
                  cityName: res.cityName,
                  countyName: res.countyName,
                  detailInfo: res.detailInfo,
                  telNumber: res.telNumber,
                  postalCode: res.postalCode
                },
                header: {
                  "content-type": "application/x-www-form-urlencoded"
                },
                method: 'POST',
                success: function (res) {
                  console.log(res)
                  that.setData({
                    "address.address_id": res.data.datas.address_id
                  })
                },
                fail: function (res) {
                  console.log(res)
                }
              })

            }
          })
        }
      }
    })

  },
  processList: function (res) {
    var datas = res.data.datas,
      totalPrice = datas.total_price,
      domain = datas.pic_domain,
      cateList = datas.cateList,
      address = datas.address;
    console.log(datas)
    var cateLists = []
    cateList.forEach(function (res) {
      var cateList = {
        name: res.goods_name,
        price: res.goods_price,
        count: res.goods_num,
        imgUrl: domain + res.original_img
      }
      cateLists.push(cateList)
    })
    if (address) {
      this.setData({
        address: address
      })
    }
    console.log(cateLists)

    this.setData({
      totalPrice: totalPrice,
      cateList: cateLists
    })
  },
  getTextareaValue: function (e) {
    this.setData({
      textareaValue: e.detail.value
    })
  }
  ,
  wxpay: function (e) {
    var that = this;
    if (!this.data.address) {
      wx.showToast({
        title: '请添加收货地址',
      })
      return;
    }
    console.log(that.data.textareaValue)
    var url = app.globalData.ydnBase + '/index.php/Weshop/SApi/index/key/2sdfdd332342341dddff46e189670/m/saveOrder/wid/39/sign/' + app.globalData.sign;
    wx.request({
      url: url,
      data: {
        u_address_id: e.currentTarget.dataset.addressId,
        msg: that.data.textareaValue,
        is_group: 0,
        is_show_addr: 1
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      success: function (res) {
        console.log("订单", res)
        if (res.data.status) {
          wx.requestPayment({
            'timeStamp': res.data.datas.timeStamp,
            'nonceStr': res.data.datas.nonceStr,
            'package': res.data.datas.package,
            'signType': res.data.datas.signType,
            'paySign': res.data.datas.paySign,
            'success': function (res) {
              console.log(res)
              wx.showToast({
                title: '支付成功',
              })
              setTimeout(function () {
                wx.switchTab({
                  url: '/pages/mine/mine',
                })
              })
            },
            'fail': function (res) {
              console.log(res)
              wx.showToast({
                title: '支付失败，请重新支付',
              })
            }

          })

        }
      },
      fail: function (res) {
        console.log(res)
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})